<?php

class LabelsController extends Controller
{
    public function actionSEO()
    {
        $model = Labels::model()->findAll();
        
        foreach ($model as $data) {
            if (!isset($data->seotool)) {
                $seotool = new SeoTools;
                $seotool->title = $data->title . ' nexttopevent - mạng sự kiện, liveshow mới nhất';
                $seotool->description = $data->title . ' thuộc về nexttopevent, mạng xã hội sự kiện cập nhật tin tức về ' . $data->title . ', xem tin tức, hình ảnh, bài viết về ' . $data->title;
                $seotool->keywords = '';
                $seotool->metarobot = '';
                $seotool->tables = 'labels';
                $seotool->table_id = $data->id;
                $seotool->status = 'A';
                $seotool->posteddate = date('Y-m-d H:i:s');
                if ($seotool->save()) {
                    echo 'Done: ' . $seotool->id . '.<br/>';
                }
            }
        }
    }
	public function actionIndex()
	{
		$model = new Labels;
        $seotool = new SeoTools;
		/*
		$criteria = new CDbCriteria;
		$criteria->condition = 'status="A" AND enddate >= NOW()';
		*/
		$dataProvider=new CActiveDataProvider('Labels',
			array(
				//'criteria'=>$criteria,
				'sort'=>array(
					'defaultOrder'=>'posteddate DESC',
				),
				'pagination'=>array(
					'pageSize'=>10,
					'pageVar'=>'page',
				),
			)
		);
		$this->render('index', array(
			'dataProvider'=>$dataProvider,
			'model'=>$model,
            'seotool'=>$seotool,
		));
	}
	
	public function actionView($slug)
	{
		$label = new EventLabels;
		$model = Labels::model()->findByAttributes(array('slug'=>$slug));
		
		$criteria = new CDbCriteria;
		$criteria->condition = 'status="A" AND enddate >= NOW()';
		$criteria->with = array('eventLabel'=>array(
			'joinType'=>'INNER JOIN',               
			'condition'=>'eventLabel.label_id = '.$model->id,
            'together'=>true
		));

		$dataProviderEvent=new CActiveDataProvider('Events',
			array(
				'criteria'=>$criteria,
				'sort'=>array(
					'defaultOrder'=>'posteddate DESC',
				),
				'pagination'=>array(
					'pageSize'=>10,
					'pageVar'=>'page',
				),
			)
		);
		
		$criteria = new CDbCriteria;
		$criteria->condition = 'status="A"';
		$criteria->with = array('newsLabel'=>array(
			'joinType'=>'INNER JOIN',               
			'condition'=>'newsLabel.label_id = '.$model->id,
            'together'=>true
		));

		$dataProviderNews=new CActiveDataProvider('News',
			array(
				'criteria'=>$criteria,
				'sort'=>array(
					'defaultOrder'=>'posteddate DESC',
				),
				'pagination'=>array(
					'pageSize'=>10,
					'pageVar'=>'page',
				),
			)
		);
		
		$this->render('view', array(
			'dataProviderEvent'=>$dataProviderEvent,
			'dataProviderNews'=>$dataProviderNews,
			'model'=>$model,
			'label'=>$label
		));
	}
	
	public function actionCreate()
	{
		if (isset($_POST['Labels'])) {
			$model = new Labels;
			$model->attributes = $_POST['Labels'];
			$model->slug = $this->createSlug($model->title);
			$model->posteddate = $model->updatedate = date('Y-m-d H:i:s');
			
			if ($model->validate() && $model->save()) {
                $seotool = new SeoTools;
                $seotool->attributes = $_POST['SeoTools'];
                $seotool->metarobot = '';
                $seotool->tables = 'labels';
                $seotool->table_id = $model->id;
                $seotool->status = 'A';
                $seotool->posteddate = date('Y-m-d H:i:s');
                $seotool->save();
				$this->redirect(array('/Events/labels/index'));
			}
		}
	}
    
    public function actionUpdate($slug)
	{
		$model = Labels::model()->findByAttributes(array('slug'=>$slug));
        
        if (isset($_POST['Labels'])) {
			$model->attributes = $_POST['Labels'];
			$model->slug = $this->createSlug($model->title);
			$model->updatedate = date('Y-m-d H:i:s');
			
			if ($model->validate() && $model->save()) {
                $model->seotool->attributes = $_POST['SeoTools'];
                $model->seotool->save();
                
				$this->redirect(array('/Events/labels/view', 'slug'=>$model->slug));
			}
		}
        
        $this->render('update', array(
            'model'=>$model
        ));
    }
	
	public function actionDelete($id)
	{
		$model = Labels::model()->findByPk($id)->delete();

		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}
	
	public function actionAddEvent($slug)
	{
		$model = new EventLabels;
		if (isset($_POST['EventLabels'])) {
			$model->attributes = $_POST['EventLabels'];
			if ($model->save()) {
				$this->redirect(array('/Events/labels/view', 'slug'=>$slug));
			}
		}
	}
	public function actionRemoveEvent($id)
	{
		$model = EventLabels::model()->findByAttributes(array('event_id'=>$id))->delete();
		
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}
}